<template>
  <main>
    <h1>校园零食商城管理后台 · 登录</h1>
    <div class="content">
      <div class="inner">
        <img :src="loginLogo" />
        <div class="form">
          <el-form label-width="auto">
            <el-form-item label="用户名">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input
                v-model="loginForm.password"
                placeholder="请输入密码"
                type="password"
                show-password
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button
                style="width: 100%; margin-top: 10px"
                type="primary"
                color="#2b303b"
                :disabled="disabled"
                @click="submit"
                >立即登录</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </main>
</template>
<script setup lang="ts">
import { computed, ref } from "vue";
import loginLogo from "@/assets/login.jpg";
import { ElMessage } from "element-plus";
import router from "@/router";
import { login } from "@/api/user";
import { useUserStore } from "@/stores/userInfo";
const store = useUserStore();
const loginForm = ref({
  username: "",
  password: "",
});
const disabled = computed(() => {
  return !loginForm.value.username || !loginForm.value.password;
});
const submit = async () => {
  let res = await login({
    userName: loginForm.value.username,
    password: loginForm.value.password,
  });
  if (res.success) {
    store.userName = res.data.userName;
    store.createTime = res.data.createTime;
    store.token = res.data.token;
    ElMessage.success("登录成功");
    router.push("/");
  } else {
    ElMessage.error(res.message);
  }
};
</script>
<style scoped lang="less">
main {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .content {
    .inner {
      width: 1300px;
      margin: 0 auto;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        height: 50vh;
        margin-right: 150px;
      }
    }
    width: 100%;
    height: 50vh;
    background-color: #fff;
  }
  .form {
    border: 1px solid #d3d3d3;
    padding: 60px 50px;
    border-radius: 10px;
  }
}
</style>
